package com.efast.dmom.mes.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.portal.entity.common.PortalAttachment;
import com.efast.cafe.portal.entity.common.PortalAttachmentForm;
import com.efast.cafe.portal.remote.common.PortalAttachmentService;
import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.Label;
import com.efast.dmom.mes.bean.LabelParam;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.bean.PortalAttachmentDataHandler;
import com.efast.dmom.mes.datahandlers.entity.LabelDataHandler;
import com.efast.dmom.mes.datahandlers.entity.LabelParamDataHandler;
import com.efast.dmom.mes.entity.LabelEntity;
import com.efast.dmom.mes.entity.LabelParamEntity;
import com.efast.dmom.mes.service.LabelService;
import com.efast.dmom.mes.util.PrintCardUtil;
import com.efast.dmom.mes.util.StringVerifyUtil;

@Service
public class LabelServiceImpl implements LabelService {

	@Autowired
	private LabelDataHandler labelDataHandler;

	@Autowired
	private LabelParamDataHandler labelParamDataHandler;

	@Autowired
	private PortalAttachmentService portalAttachmentService;
	
	@Autowired
	private PortalAttachmentDataHandler portalAttachmentDataHandler;
	
	@Override
	public ResultBean save(Label label) {
		ResultBean resultBean = new ResultBean(true, ' ');
		LabelEntity labelEntity = label.getInnerData();
		if (labelEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的信息为空！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelEntity.getLabelCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签编号是否填写！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelEntity.getLabelType())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签类型是否填写！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelEntity.getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签名称是否填写！");
			return resultBean;
		}
		Label labelByLabelCode = labelDataHandler.getByCode(labelEntity.getLabelCode());
		if (labelByLabelCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("标签编号不唯一！");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(labelEntity.getLabelCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("标签编号不是由英文、字母、下划线组成！");
			return resultBean;
		}
		if (label.getAttachments() != null && label.getAttachments().size() > 0) {
			String formId = UUID.randomUUID().toString();
			labelEntity.setGenerateFormId(formId);
			for (PortalAttachmentForm data : label.getAttachments()) {
				data.setFormId(formId);
			}
			portalAttachmentService.saveAttachmentForm(labelEntity.getGenerateFormId(), label.getAttachments());
		}
		ResultBean resBean = labelDataHandler.save(label);
		return resBean;
	}

	@Override
	public ResultBean update(Label label) {
		ResultBean resultBean = new ResultBean(true, ' ');
		LabelEntity labelEntity = label.getInnerData();
		if (labelEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("更新的信息为空！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelEntity.getLabelCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签编号是否填写！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelEntity.getLabelType())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签类型是否填写！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelEntity.getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签名称是否填写！");
			return resultBean;
		}
		Label labelById = labelDataHandler.getById(labelEntity.getId());
		if (!labelById.getInnerData().getLabelCode().equals(labelEntity.getLabelCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("标签编号不可修改！");
			return resultBean;
		}
		if (label.getAttachments() != null && label.getAttachments().size() > 0) {
			boolean flag = true;
			for (PortalAttachmentForm data : label.getAttachments()) {
				if (label.getAttachmentId() != data.getAttachmentId()) {
					flag = false;
					break;
				}
			 } 
			if (!flag) {
			String formId = UUID.randomUUID().toString();
			for (PortalAttachmentForm data : label.getAttachments()) {
				data.setFormId(formId);
			}
			labelEntity.setGenerateFormId(formId);
			portalAttachmentService.saveAttachmentForm(labelEntity.getGenerateFormId(), label.getAttachments());
		  }
		}
		ResultBean resBean = labelDataHandler.update(label);
		return resBean;
	}

	@Override
	public ResultBean delete(Label label) {
		ResultBean resultBean = new ResultBean(true, ' ');
		String message = "";
		LabelEntity labelEntity = label.getInnerData();
		String labelTemplateId = labelEntity.getGenerateFormId();
		if (labelTemplateId != null && labelTemplateId.length() != 0) {
			message = "该标签数据中存在标签模板";
		}
		List<LabelParam> paramList = labelParamDataHandler.getByLabelId(labelEntity.getId());
		if (paramList != null && paramList.size() > 0) {
			if (message.length() != 0 && message != null) {
				resultBean.setSuccess(false);
				resultBean.setObject(label);
				resultBean.setMessage(message + "和子项数据，是否删除？");
				return resultBean;
			} else if (message == null || "".equals(message)) {
				resultBean.setSuccess(false);
				resultBean.setObject(label);
				resultBean.setMessage("该标签数据存在子项数据，是否删除？");
				return resultBean;
			}
		} else {
			if (message.length() != 0 && message != null) {
				resultBean.setSuccess(false);
				resultBean.setObject(label);
				resultBean.setMessage("该标签数据中存在标签模板，是否删除？");
				return resultBean;
			}
		}
		ResultBean resBean = labelDataHandler.delete(label);
		return resBean;
	}

	public ResultBean deleteLabelAndLabelParam(Label label) {
		ResultBean resultBean = new ResultBean(true, ' ');
		labelDataHandler.delete(label);
		LabelParam labelParam = new LabelParam();
		LabelParamEntity labelParamEntity = new LabelParamEntity();
		labelParamEntity.setLabelId(label.getInnerData().getId());
		labelParam.setInnerData(labelParamEntity);
		labelParamDataHandler.deleteByLabelId(labelParam);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
	}

	@Override
	public ResultBean previewLable(long labelId, HttpServletResponse response) {
		ResultBean resultBean = new ResultBean(true,' ');
		List<Label> reportList=null;
		Map<String, Object> parameters = new HashMap<String, Object>();
		List<LabelParam> labelParamList = labelParamDataHandler.getByLabelId(labelId);
		PortalAttachment attachmentBean = portalAttachmentDataHandler.findAttach(labelId);
		String filePath = " ";
		if (attachmentBean != null) {
			filePath = attachmentBean.getFilePath()+attachmentBean.getSysName();
		}
		if (labelParamList != null && labelParamList.size() > 0) {
			for (LabelParam labelParam:labelParamList) {
				LabelParamEntity labelParamEntity = labelParam.getInnerData();
				if (labelParamEntity.getIsFixed() == 1) {
					parameters.put(labelParamEntity.getLabelParamCode(),labelParamEntity.getLabelParamValue());
				}
			}
		}
		PrintCardUtil<Label> util = new PrintCardUtil<>(reportList);
		util.iReportWebPrintResponse(parameters, filePath, response);
		return resultBean;
	}

}
